package com.sunducation.wts.penbox.presenter;

import android.content.Context;

import com.sunducation.wts.penbox.contract.WorkRecordDetailContract;
import com.wetao.lib_common.bean.response.ExamDetailInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.JsonUtils;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * author: zhouyh
 * created on: 3/10/21 11:16 AM
 * description:
 */
public class WorkRecordDetailPresenter extends WorkRecordDetailContract.presenter {

    private Context mContext;

    public WorkRecordDetailPresenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void queryMarkingDetail(HashMap<String, String> map, boolean isDialog, boolean cancelable) {

        String json =(JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().queryMarkingDetail(requestBody)
                .retryWhen(new RetryWithDelay(2,1))
                .compose(RxUtils.<ExamDetailInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<ExamDetailInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<ExamDetailInfo>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(ExamDetailInfo baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().queryMarkingDetailResult(baseResponse);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().queryMarkingDetailFail();
                    }
                });

    }
}
